#!/bin/sh

# Slackware build script for sdlmame data

# Written by Pablo Santamaria (pablosantamaria@gmail.com)

PRGNAM=sdlmame-data
VERSION=${VERSION:-0.136}
SRCVER=$( echo $VERSION | cut -d. -f2 )
ARCH=noarch
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

SOURCE1="mamehistory$SRCVER.zip"
SOURCE2="Mameinfo0$SRCVER.zip"
SOURCE3="ctrlr.rar"
SOURCE4="catveren.zip"
SOURCE5="nplayers0$SRCVER.zip"
SOURCE6="cheat0$SRCVER.zip"

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
mkdir $PRGNAM-$VERSION
cd $PRGNAM-$VERSION

# Extract dat file
unzip $CWD/$SOURCE1
unzip $CWD/$SOURCE2
7z x Mameinfo0$SRCVER.exe
mv docs mameinfo
unzip -a $CWD/$SOURCE4
mkdir -p catveren
mv readme.txt catveren
unzip -a $CWD/$SOURCE5
mv docs nplayers
unzip $CWD/$SOURCE6

# Fix permissions and line endings
chmod 0644 mameinfo/*.txt
chmod 0755 mameinfo
sed -i 's/\r//' mameinfo/* nplayers/nplayers.txt

# Install
install -d $PKG/usr/share/games/mame
install -pm 644 history.dat mameinfo.dat Catver.ini nplayers.ini cheat.zip \
    $PKG/usr/share/games/mame
7z x -o$PKG/usr/share/games/mame $CWD/$SOURCE3

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  catveren/readme.txt nplayers/nplayers.txt mameinfo cheat.txt readhist.txt \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
